package com.konsung.ksdc.manager.shop;

import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.OrderProductNetBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 订单详情商品列表adapter
 * Created by Administrator on 2016/5/3 0003.
 */
public class OrderBuyListAdapter extends BaseAdapter {

    public static final String ORDER_PRODUCT_ID = "order_product_id";
    public static final String ORDER_PRODUCT_BEAN = "order_product_bean";
    private List<OrderProductNetBean> orderProductNetBeanList;
    private final String orderStatus;
    private boolean showBackground;

    /**
     * 构造方法
     * @param list 商品列表集合
     * @param status 订单状态
     * @param background 退换货状态背景
     */
    public OrderBuyListAdapter(List<OrderProductNetBean> list, String status, boolean background) {
        orderProductNetBeanList = list;
        orderStatus = status;
        this.showBackground = background;
    }

    @Override
    public int getCount() {
        if (orderProductNetBeanList != null) {
            return orderProductNetBeanList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return orderProductNetBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(MyApplication.getContext())
                    .inflate(R.layout.item_product_for_confirm_order, null);
            ButterKnife.bind(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final OrderProductNetBean bean = orderProductNetBeanList.get(position);

        String iconImageUrl = bean.getProductIconPath();
        Picasso.with(MyApplication.getContext())
                .load(StringUtils.appendPicPath(iconImageUrl))
                .placeholder(R.drawable.pic_talkbox_sel)
                .error(R.drawable.pic_talkbox_sel)
                .into(holder.ivProduct);

        String title = bean.getProductName();
        holder.tvTitle.setText(title);

        String subTitle = bean.getSubTitle();
        holder.tvSubtitle.setText(subTitle);

        int count = Integer.parseInt(bean.getShoppingNumber());
        holder.tvNum.setText("" + count);

        // ### 展示退换货进度 ###
        final String status = bean.getStatus();

        //根据订单状态显示退换货按钮
        if (Constants.ORDER_TYPE_NOT_COMMENT.equals(orderStatus) || Constants.ORDER_TYPE_SUCCESS
                .equals(orderStatus)) {
            holder.tvRequestState.setVisibility(View.VISIBLE);
            if (showBackground) {
                holder.tvRequestState.setBackgroundResource(R.drawable.bg_btn_text);
                holder.tvRequestState.setTextColor(Color.parseColor("#666666"));
            } else {
                holder.tvRequestState.setBackgroundResource(R.color.bg_order_item);
                holder.tvRequestState.setTextColor(Color.parseColor("#d14040"));
            }
            /**
             * 订单商品状态(1、正常,2、退货中，3、换货中，4、退货成功，5、换货成功,
             * 6、已取消7、申请退货，8、申请换货, 9、退-审核通过,0、换-审核通过)
             */
            switch (status) {
                case Constants.PRODUCT_TYPE_NORMAL:
                    holder.tvRequestState.setText(R.string.apply_returns);
                    if (showBackground) {
                        holder.tvRequestState.setVisibility(View.VISIBLE);
                    } else {
                        holder.tvRequestState.setVisibility(View.GONE);
                    }
                    break;
                case Constants.PRODUCT_TYPE_REQUEST_REFUND:
                    holder.tvRequestState.setText(R.string.returning);
                    break;

                case Constants.PRODUCT_TYPE_REFUND_CHECKED:
                    holder.tvRequestState.setText(R.string.returning);
                    break;

                case Constants.PRODUCT_TYPE_REFUNDING:
                    holder.tvRequestState.setText(R.string.returning);
                    break;

                case Constants.PRODUCT_TYPE_REFUNDING_SUCCESS:
                    holder.tvRequestState.setText(R.string.btn_refund_success);
                    break;

                case Constants.PRODUCT_TYPE_REQUEST_EXCHANGE:
                    holder.tvRequestState.setText(R.string.btn_exchanging);
                    break;

                case Constants.PRODUCT_TYPE_EXCHANGE_CHECKED:
                    holder.tvRequestState.setText(R.string.btn_exchanging);
                    break;

                case Constants.PRODUCT_TYPE_EXCHANGING:
                    holder.tvRequestState.setText(R.string.btn_exchanging);
                    break;

                case Constants.PRODUCT_TYPE_EXCHANGING_SUCCESS:
                    holder.tvRequestState.setText(R.string.title_exchange_success);
                    break;
                default:
                    holder.tvRequestState.setVisibility(View.GONE);
                    break;
            }
        } else {
            holder.tvRequestState.setVisibility(View.GONE);
        }
        if (showBackground) {
            holder.tvRequestState.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Constants.PRODUCT_TYPE_NORMAL.equals(status)) {
                        Intent intent = new Intent(MyApplication.getContext(),
                                ApplyReturnsActivity.class);
                        intent.putExtra(ORDER_PRODUCT_ID, bean.getId());
                        intent.putExtra(ORDER_PRODUCT_BEAN, bean);
                        MyApplication.mCurrentActivity.startActivity(intent);
                    } else {
                        Intent intent = new Intent(MyApplication.getContext(),
                                ReturnsDetailActivity.class);
                        intent.putExtra(ORDER_PRODUCT_ID, bean.getId());
                        intent.putExtra(ORDER_PRODUCT_BEAN, bean);
                        MyApplication.mCurrentActivity.startActivity(intent);
                    }
                }
            });
        }

        String price = bean.getFavor();
        holder.tvPrice.setText("￥" + price);

        return convertView;
    }

    /**
     * 适配器
     */
    static class ViewHolder {
        @Bind(R.id.iv_product)
        ImageView ivProduct;
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_subtitle)
        TextView tvSubtitle;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_num)
        TextView tvNum;
        @Bind(R.id.tv_request_state)
        TextView tvRequestState;
    }

    /**
     * 手动设置数据
     * @param data 商品列表集合
     */
    public void setData(List<OrderProductNetBean> data) {
        orderProductNetBeanList = data;
        notifyDataSetChanged();
    }
}
